package com.youlu.campus.admin.community.controller;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.ActivityCostStatistics;
import com.youlu.campus.entity.ActivityCostStatisticsQueryVO;
import com.youlu.campus.entity.VO.req.ActivityStatisticsQueryVO;
import com.youlu.campus.service.activity.ActivityCostStatisticsService;
import com.youlu.campus.service.activity.ActivityStatisticsService;
import com.yuelin.infrastructure.quantum.common.QResult;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class ActivityStatisticsController {
    @Autowired
    private ActivityStatisticsService activityStatisticsService;
    @Autowired
    private ActivityCostStatisticsService activityCostStatisticsService;

    @GetMapping("/activity-statistics/init/type")
    public QResult initType() {
        try {
            activityStatisticsService.initActivityStatisticsType();
            return QResult.success(null);
        } catch (Exception e) {
            log.error(":>>> 初始化活动统计增加类型错误:{}", e);
            return QResult.fail("活动统计详情错误");
        }
    }



    @PostMapping("/activity-statistics/list")
    public QResult activityList(@RequestBody ActivityStatisticsQueryVO request) {
        try {
            request.setUserId(AuthUtils.getUserId());
            return QResult.success(activityStatisticsService.list(request));
        } catch (Exception e) {
            log.error(":>>> 活动列表统计错误:{}", e);
            return QResult.fail("活动统计列表错误");
        }
    }

    @GetMapping("/activity-statistics/find-one")
    public QResult findOne(@RequestParam("id") String id) {
        try {
            return QResult.success(activityStatisticsService.findOne(id, AuthUtils.getLoginName()));
        } catch (Exception e) {
            log.error(":>>> 活动列表统计详情:{}", e);
            return QResult.fail("活动统计详情错误");
        }
    }

    @PostMapping("/activity-statistics/cost/list")
    public QResult costList(@RequestBody ActivityCostStatisticsQueryVO request) {
        try {
            return QResult.success(activityCostStatisticsService.list(request));
        } catch (Exception e) {
            log.error(":>>> 活动列表统计错误:{}", e);
            return QResult.fail("活动成本列表错误");
        }

    }

    @PostMapping("/activity-statistics/cost/create")
    public QResult createCost(@RequestBody ActivityCostStatistics request) {
        try {
            request.setOptLoginName(AuthUtils.getLoginName());
            activityCostStatisticsService.create(request);
            return QResult.success("ok");
        } catch (Exception e) {
            log.error(":>>> 创建活动成本错误错误:{}", e);
            return QResult.fail("创建活动成本错误错误");
        }

    }

    @PostMapping("/activity-statistics/cost/update")
    public QResult updateCost(@RequestBody ActivityCostStatistics request) {
        boolean re = activityCostStatisticsService.update(request);

        if (re) {
            return QResult.success("ok");
        }
        return QResult.fail("no");

    }

    @GetMapping("/activity-statistics/cost/delete")
    public QResult deleteCost(@RequestParam("id") String id) {
        boolean re = activityCostStatisticsService.delete(id);
        if (re) {
            return QResult.success("ok");
        }
        return QResult.fail("no");
    }

    @GetMapping("/activity-statistics/cost/find-one")
    public QResult getCost(@RequestParam("id") String id) {
        try {
            ActivityCostStatistics da = activityCostStatisticsService.findOne(id);
            return QResult.success(da);
        } catch (Exception e) {
            log.error(":>>> 查找活动成本错误错误:{}", e);
            return QResult.fail("查找活动成本错误错误");
        }
    }

    @GetMapping("/activity-statistics/cost/gmv")
    public QResult getGMV(@RequestParam("id") String id) {
        try {
            ActivityCostStatistics da = activityCostStatisticsService.findOne(id);
            return QResult.success(da);
        } catch (Exception e) {
            log.error(":>>> 查找活动成本错误错误:{}", e);
            return QResult.fail("查找活动成本错误错误");
        }
    }

    @PostMapping("/activity-statistics/update/user/sign-up-count")
    public RestResult updateUserSignUpCount(@RequestBody ActivityStatisticsQueryVO request){
        activityStatisticsService.updateUserSignUpCount(request.getId(),request.getUserSignUpCount());
        return RestResultBuilder.builder().success().data(null).build();
    }
}
